/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.geoviewer.jxmap.layerlist;

import java.beans.IntrospectionException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.Action;
import nl.cloudfarming.client.geoviewer.Layer;
import nl.cloudfarming.client.geoviewer.LayerAction;
import nl.cloudfarming.client.geoviewer.MapController;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.Utilities;
import org.openide.util.lookup.Lookups;

/**
 *
 * @author Timon Veenstra
 */
public class LayerListLayerNode extends AbstractNode {

    private final Layer layer;

    public LayerListLayerNode(Layer layer, MapController mapController) throws IntrospectionException {
        super(Children.LEAF, Lookups.fixed(layer, mapController));
        this.layer = layer;
        setValue(AbstractNode.PROP_NAME, layer.getName());
        setValue(AbstractNode.PROP_DISPLAY_NAME, layer.getName());
    }

    @Override
    public Action[] getActions(boolean context) {
        List<LayerAction> globalActions = new ArrayList<LayerAction>();
        // first add all global layer actions from the lookup
        for (LayerAction action: Lookup.getDefault().lookupAll(LayerAction.class)){
            try {
                // new instance needed because otherwise the same instance will be used for all
                // layers which makes it impossible to change things according to layer state
                globalActions.add(action.getClass().newInstance());
            } catch (InstantiationException ex) {
                Exceptions.printStackTrace(ex);
            } catch (IllegalAccessException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
        //
        // add layer specific actions
        //
        LayerAction[] specificActions = getLayer().getActions();
        if (specificActions != null && specificActions.length > 0) {
            globalActions.addAll(Arrays.asList(specificActions));
        }
        return globalActions.toArray(new Action[globalActions.size()]);
    }

    public Layer getLayer() {
        return this.layer;
    }

    @Override
    public String getHtmlDisplayName() {
        Layer selected = Utilities.actionsGlobalContext().lookup(Layer.class);
        if (selected == null || !selected.equals(getLayer())) {
            return getLayer().getName();
        } else {
            StringBuilder sb = new StringBuilder("<b>");
            sb.append(getLayer().getName());
            sb.append("</b>");
            return sb.toString();
        }

    }
}
